/**
 * Copyright © 2011 looty - Textoo
 * @link http://www.textoo.org
 * @link http://code.google.com/p/textoo/
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @version 2.0
 */

package org.looty.core.textoo.context.local 
{
	import flash.events.*;
	import flash.external.*;
	import flash.system.*;
	import org.looty.log.*;
	import org.looty.textoo.events.*;
	
	/**
	 * Localisation langage code is built from the langage code defined in the <a href="http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes" >ISO 639</a> standard along with the country code defined in the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2" >ISO 3166</a>. eg : en_UK, fr_FR
	 */

	public class LanguageManager extends EventDispatcher
	{
		
		private var _languages		:Vector.<String>;
		private var _index			:int;
		private var _code			:String;
		private var _isInjected		:Boolean;
		
		private var _addedEvent		:TextooEvent;
		private var _changeEvent	:TextooEvent;
		
		public function LanguageManager() 
		{
			_languages = new Vector.<String>();
			_index = -1;
			_code = "";
			
			_addedEvent = new TextooEvent(TextooEvent.LANGUAGE_ADDED);
			_changeEvent = new TextooEvent(TextooEvent.LANGUAGE_CHANGE);
		}
		
		public function add(code:String):uint
		{
			code = code.toLowerCase();
			var i:int = _languages.indexOf(code);
			if (i != -1) return i;
			
			_languages.fixed = false;
			_languages.push(code);
			_languages.fixed = true;
			
			dispatchEvent(_addedEvent);
			
			if (_code != "" && setLanguage(_code)) _code = "";
			else if (_index == -1) setLanguage(_languages[0]); //set first language by default...
			
			return _languages.length - 1;
		}
		
		public function hasLanguage(code:String):Boolean
		{
			return getLanguageIndex(code) != -1;
		}
		
		private function getLanguageIndex(code:String):int
		{
			code = code.toLowerCase();
			
			var i:int = _languages.indexOf(code);
			if (i != -1) return i;
			
			code = code.substr(0, 2);
			
			i = _languages.indexOf(code);
			if (i != -1) return i;
			
			if (_languages.length > 0) for (i = 0; i < _languages.length; ++i) if (_languages[i].substr(0, 2) == code) return i;
			
			return -1;
		}
		
		public function get language():String
		{
			return _index > -1 ? _languages[_index] : "none";
		}
		
		public function setLanguage(code:String):Boolean
		{
			var i:int = getLanguageIndex(code);
			if (_index == i) return false;
			
			if (i != -1) 
			{
				_index = i;
				Looger.info("language : " + _languages[_index]);
				dispatchEvent(_changeEvent);
				return true;
			}
			else 
			{
				_code = code;
				return false;
			}
		}
		
		public function get index():int 
		{
			return _index;
		}
		
		public function get languages():Vector.<String> 
		{
			return _languages.slice();
		}
		
		public function get length():uint
		{
			return _languages.length;
		}
		
		public function detectLanguage():Boolean
		{			
			/*
			*/
			
			var code:String
			
			if (ExternalInterface.available)
			{
				if (!_isInjected)
				{
					var js:XML=
						<script>
							<![CDATA[
							function ()
							{
								getLanguage = function () 
								{
									return (navigator.appName == 'Netscape') ? navigator.language : navigator.browserLanguage;
								}
							}
							]]>
						</script>;
					ExternalInterface.call(js);
					_isInjected = true;
				}			
				
				code = ExternalInterface.call("getLanguage");
				
				if (code != null && hasLanguage(code))
				{
					Looger.info("navigator language : " + code);
					setLanguage(code);
					return true;
				}
			}
			
			code = Capabilities.language;
			
			if (hasLanguage(code))
			{
				Looger.info("system language : " + code);
				setLanguage(code);
				return true;
			}
			
			return false;
		}
		
		public function dispose():void
		{
			_addedEvent =  null;
			_changeEvent = null;
			_languages.length = 0;
			_languages = null;
		}
		
		public function dump():void 
		{
			Looger.info(" -- languages --");
			for (var i:int = 0; i < _languages.length; i++) Looger.info(" [" + i + "] " + _languages[i]); 
			Looger.info(" current language index : " + _index);
		}
		
		
		
	}

}